package com.system.vote.admin.controller.app;


import com.system.vote.core.captcha.CreateImageCode;
import com.system.vote.core.http.Result;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * 验证码Controller控制器
 */
@RestController
@Api(tags = "App验证码")
@Slf4j
public class AppCaptchaController {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @GetMapping("/app/vote/tWxUserinfo/captcha")
    public Result captcha() throws IOException {

        CreateImageCode createImageCode = new CreateImageCode();
        Map<String, String> imgObj = createImageCode.getCodeString(140, 30, 4);
        String key = UUID.randomUUID().toString();
        redisTemplate.opsForValue().set(key, imgObj.get("text").toLowerCase(), 5, TimeUnit.MINUTES);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("captchaImg", imgObj.get("base64Img"));
        resultMap.put("uuid", key);
        resultMap.put("captcha",imgObj.get("text"));
        return Result.success(resultMap);
    }
}
